package com.hxm.appGenealogy.service;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxm.appGenealogy.Function.CommentFunction;
import com.hxm.appGenealogy.Function.DynamicFunction;
import com.hxm.appGenealogy.config.ParmIntended;
import com.hxm.appGenealogy.config.queryVo.QueryWrapperVO;
import com.hxm.appGenealogy.entity.Comment;
import com.hxm.appGenealogy.entity.Dynamic;
import com.hxm.appGenealogy.entity.DynamicVo;
import com.hxm.appGenealogy.entity.ResponDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW)
public class CommentService {

    @Autowired
    private CommentFunction commentFunction;


    @Autowired
    private DynamicFunction dynamicFunction;
    //添加评论;
    public ResponDataVo insertComment(Comment comment){
        //参数教验;
        String parmAnalysis = ParmIntended.parmAnalysis(comment, "dynamicId", "peopleId", "content");
        if(parmAnalysis!=null){
            return new ResponDataVo(parmAnalysis);
        }
        //添加评论数据;
        comment.setTime(new Date());
        commentFunction.insertDynamic(comment);
        //添加评论的量;
        Long dynamicId = comment.getDynamicId();
        Dynamic dynamic = dynamicFunction.selectByid(dynamicId);
        dynamic.setCommentQuantity(dynamic.getCommentQuantity()+1);
        dynamicFunction.updateByid(dynamic);
        return new ResponDataVo(comment);
    }

    /**
     * 查询评论的数据;
     * @param dynamic
     * @return
     */
    public ResponDataVo commentList(DynamicVo dynamic){
        String parmAnalysis = ParmIntended.parmAnalysis(dynamic, "id");
        if(parmAnalysis!=null){
            return new ResponDataVo(parmAnalysis);
        }
        Page page = new Page();
        page.setCurrent(dynamic.getCurrent());
        page.setSize(dynamic.getSize());
        Page page1 = commentFunction.selectPage(page, JSON.toJSONString(new QueryWrapperVO().eq("dynamic_id", dynamic.getId())));
        Map<String,Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("total",page1.getTotal());
        stringObjectMap.put("commentList",page1.getRecords());
        return new ResponDataVo(stringObjectMap);
    }

}
